package com.dream.myqiyi.ui;

import com.dream.myqiyi.R;
import com.dream.myqiyi.service.ApplicationContext;
import com.dream.myqiyi.service.NotebookService;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

public class NotebookViewActivity extends Activity {

	private EditText mNotebookNameEdit;
	private String notebookName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notebook_view_activity);
		
		mNotebookNameEdit = (EditText)findViewById(R.id.notebook_view_name_edit);
		
		findViewById(R.id.notebook_view_ok_btn).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if("".equals(mNotebookNameEdit.getText().toString())) {
		        	new AlertDialog.Builder(NotebookViewActivity.this)
					.setIcon(getResources().getDrawable(R.drawable.login_error_icon))
					.setTitle("创建记事本错误")
					.setMessage("记事本名字为空！")
					.create().show();
				} else {
					notebookName = mNotebookNameEdit.getText().toString();
					new SaveNotebooksTask().execute("");
					
					Intent intent = new Intent (NotebookViewActivity.this, NotebookListActivity.class);			
					startActivity(intent);			
					NotebookViewActivity.this.finish();

					//MainActivity.instance.replaceView(1);		
				}	
			}			
		});

		findViewById(R.id.notebook_view_reback_btn).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent (NotebookViewActivity.this, NotebookListActivity.class);			
				startActivity(intent);			
				NotebookViewActivity.this.finish();

				//MainActivity.instance.replaceView(1);		
			}
			
		});

		//ononein();
	}

	private class SaveNotebooksTask extends AsyncTask<String, String, String> {
		public String doInBackground(String... params) {	
			if (!notebookName.equals("")) {
				NotebookService nbService = (NotebookService)ApplicationContext.getSingleton().getBean("NotebookService");
				if (nbService != null) {
					nbService.addNotebook(notebookName);
				}
			}

			return "";
		}

		@Override
		public void onPostExecute(String Re) {
		}
	}
}
